﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Web.SessionState;
using Arizu.Repository;
using System.Data;

namespace AiRiZu.User.handler
{
    public class GetRoomFixedPriceInfo : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            if (context.Request.Params["roomId"] == null || string.IsNullOrEmpty(context.Request.Params["roomId"].ToString()))
            {
                context.Response.Write("error");
                return;
            }

            StringBuilder sBuilder = new StringBuilder();
            long roomId = long.Parse(context.Request.Params["roomId"].ToString());

            AirizuPrice airizuPrice = AirizuPrice.CreateInstance();

            DataTable dt = airizuPrice.GetPrice(roomId);

            if (dt.Rows.Count > 0)
            {
                sBuilder.Append("{");
                sBuilder.Append("\"roomTitle\":").Append("\"").Append(roomId).Append("\", ");
                if (dt.Rows[0]["DayPrice"].ToString().Equals("0"))
                {
                    sBuilder.Append("\"pricePerNight\":").Append("\"").Append("").Append("\", ");
                }
                else
                {
                    sBuilder.Append("\"pricePerNight\":").Append("\"").Append(dt.Rows[0]["DayPrice"].ToString()).Append("\", ");
                }

                if (dt.Rows[0]["CheckIn"].ToString().Equals("0"))
                {
                    sBuilder.Append("\"checkin\":").Append("\"").Append("mm/dd/yyyy").Append("\", ");
                }
                else
                {
                    sBuilder.Append("\"checkin\":").Append("\"").Append(dt.Rows[0]["CheckIn"].ToString()).Append("\", ");
                }

                if (dt.Rows[0]["CheckOut"].ToString().Equals("0"))
                {
                    sBuilder.Append("\"checkout\":").Append("\"").Append("mm/dd/yyyy").Append("\", ");
                }
                else
                {
                    sBuilder.Append("\"checkout\":").Append("\"").Append(dt.Rows[0]["CheckOut"].ToString()).Append("\", ");
                }

                if (dt.Rows[0]["AdvancedPrice"].ToString().Equals("0"))
                {
                    sBuilder.Append("\"advancedPricing\":").Append("\"").Append("").Append("\", ");
                }
                else
                {
                    sBuilder.Append("\"advancedPricing\":").Append("\"").Append(dt.Rows[0]["AdvancedPrice"].ToString()).Append("\", ");
                }

                if (dt.Rows[0]["WeekPrice"].ToString().Equals("0"))
                {
                    sBuilder.Append("\"weeklyPricing\":").Append("\"").Append("").Append("\", ");
                }
                else
                {
                    sBuilder.Append("\"weeklyPricing\":").Append("\"").Append(dt.Rows[0]["WeekPrice"].ToString()).Append("\", ");
                }

                if (dt.Rows[0]["AdditionalWeeklyPrice"].ToString().Equals("0"))
                {
                    sBuilder.Append("\"additionalWeeklyPricing\":").Append("\"").Append("").Append("\", ");
                }
                else
                {
                    sBuilder.Append("\"additionalWeeklyPricing\":").Append("\"").Append(dt.Rows[0]["AdditionalWeeklyPrice"].ToString()).Append("\", ");
                }

                if (dt.Rows[0]["MonthPrice"].ToString().Equals("0"))
                {
                    sBuilder.Append("\"monthlyPricing\":").Append("\"").Append("").Append("\", ");
                }
                else
                {
                    sBuilder.Append("\"monthlyPricing\":").Append("\"").Append(dt.Rows[0]["MonthPrice"].ToString()).Append("\", ");
                }

                if (dt.Rows[0]["AdditionalMonthlyPrice"].ToString().Equals("0"))
                {
                    sBuilder.Append("\"additionalMonthlyPricing\":").Append("\"").Append("").Append("\", ");
                }
                else
                {
                    sBuilder.Append("\"additionalMonthlyPricing\":").Append("\"").Append(dt.Rows[0]["AdditionalMonthlyPrice"].ToString()).Append("\", ");
                }

                if (dt.Rows[0]["GuestNo"].ToString().Equals("0"))
                {
                    sBuilder.Append("\"additionalGuestNo\":").Append("\"").Append("").Append("\", ");
                }
                else
                {
                    sBuilder.Append("\"additionalGuestNo\":").Append("\"").Append(dt.Rows[0]["GuestNo"].ToString()).Append("\", ");
                }
                
                if (dt.Rows[0]["GuestPrice"].ToString().Equals("0"))
                {
                    sBuilder.Append("\"additionalGuestPricing\":").Append("\"").Append("").Append("\", ");
                }
                else
                {
                    sBuilder.Append("\"additionalGuestPricing\":").Append("\"").Append(dt.Rows[0]["GuestPrice"].ToString()).Append("\", ");
                }

                if (dt.Rows[0]["Fee"].ToString().Equals("0"))
                {
                    sBuilder.Append("\"additionalFee\":").Append("\"").Append("").Append("\", ");
                }
                else
                {
                    sBuilder.Append("\"additionalFee\":").Append("\"").Append(dt.Rows[0]["Fee"].ToString()).Append("\", ");
                }
                
                sBuilder.Append("}");
            }
            else
            {
                sBuilder.Append("error");
            }
            context.Response.Write(sBuilder.ToString());
        }

        private void test()
        {
            StringBuilder sBuilder = new StringBuilder();
            sBuilder.Append("{");
            sBuilder.Append("\"roomTitle\":").Append("\"").Append("").Append("\", ");
            sBuilder.Append("\"pricePerNight\":").Append("\"666666666\", ");
            sBuilder.Append("\"checkin\":").Append("\"111\", ");
            sBuilder.Append("\"checkout\":").Append("\"222222\", ");
            sBuilder.Append("\"advancedPricing\":").Append("\"99999999999999999999\", ");
            sBuilder.Append("\"weeklyPricing\":").Append("\"123\", ");
            sBuilder.Append("\"additionalWeeklyPricing\":").Append("\"123\", ");
            sBuilder.Append("\"monthlyPricing\":").Append("\"123\", ");
            sBuilder.Append("\"additionalMonthlyPricing\":").Append("\"sfd\", ");
            sBuilder.Append("\"additionalGuestNo\":").Append("\"3243\", ");
            sBuilder.Append("\"additionalGuestPricing\":").Append("\"12312\", ");
            sBuilder.Append("\"additionalFee\":").Append("\"8797897\"");
            sBuilder.Append("}");
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
